package com.example.wldcmzy.study003;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.security.PrivateKey;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

public class task01View extends AppCompatActivity {
    private static int checkBoxNum = 3;

    private ToggleButton tog;
    private Button out, clear;
    private Switch switch01, switch02;
    private TextView switchT01, switchT02, togT, massage;
    private LinearLayout linear01;
    private RadioButton radioTeacher, radioStudent, radioOthers;
    private RadioGroup radioGroup;
    private CheckBox CBkx, CBgy, CBkxn;
    private CheckBox[] checkBoxGroup = new CheckBox[checkBoxNum];
    private TreeMap<Integer, String> mapCheckBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task01_view);

        tog = (ToggleButton) findViewById(R.id.toggleButton);
        togT = (TextView) findViewById(R.id.toggleButtonT);
        switch01 = (Switch) findViewById(R.id.switch01);
        switch02 = (Switch) findViewById(R.id.switch02);
        switchT01 = (TextView) findViewById(R.id.switchT01);
        switchT02 = (TextView) findViewById(R.id.switchT02);
        linear01 = (LinearLayout) findViewById(R.id.LinearLayoutPart01);
        massage = (TextView) findViewById(R.id.massage);
        radioTeacher = (RadioButton) findViewById(R.id.radioTeacher);
        radioStudent = (RadioButton) findViewById(R.id.radioStudent);
        radioOthers = (RadioButton) findViewById(R.id.radioOthers);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        CBkx = (CheckBox) findViewById(R.id.checkBoxKEXING);
        CBgy = (CheckBox) findViewById(R.id.checkBoxGUOYAO);
        CBkxn = (CheckBox) findViewById(R.id.checkBoxKANGXINUO);
        out = (Button) findViewById(R.id.outtext);
        clear = (Button) findViewById(R.id.cleartext);

        switch01.setOnCheckedChangeListener(QswitchChangeListener);
        switch02.setOnCheckedChangeListener(QswitchChangeListener);
        tog.setOnCheckedChangeListener(QswitchChangeListener);
        out.setOnClickListener(QButtonOutOnClickListener);
        clear.setOnClickListener(QButtonOutOnClickListener);


        checkBoxGroup[0] = CBkx;
        checkBoxGroup[1] = CBgy;
        checkBoxGroup[2] = CBkxn;
        mapCheckBox = new TreeMap<Integer, String>();
        mapCheckBox.put(0, "科兴");
        mapCheckBox.put(1, "国药");
        mapCheckBox.put(2, "康希诺");
    }

   private View.OnClickListener QButtonOutOnClickListener
            = new View.OnClickListener(){
        public void onClick(View v){
            int id = v.getId();
            switch(id){
                case R.id.outtext:
                    String res = "", email = "";
                    switch(radioGroup.getCheckedRadioButtonId()){
                        case R.id.radioTeacher:
                            res += "你的身份是老师, ";
                            break;
                        case R.id.radioStudent:
                            res += "你的身份是学生, ";
                            break;
                        case R.id.radioOthers:
                            res += "你的身份是非老师学生的其他人员, ";
                            break;
                        default:
                            res += "你尚未选择身份.";
                            break;
                    }
                    for(int i = 0; i < checkBoxNum; i += 1){
                        if(checkBoxGroup[i].isChecked()){
                            if(!email.equals("")){
                                email += '、';
                            }
                            email += mapCheckBox.get(i);
                        }
                    }
                    if(email.equals("")){
                        res += "你尚未选中任何疫苗,";
                    }else{
                        res += "你打了" + email + "的疫苗, ";
                    }
                    if(switch01.isChecked()){
                        res += "你需要排队板凳";
                    }else{
                        res += "你不需要排队板凳";
                    }
                    res += ",";
                    if(switch02.isChecked()){
                        res += "你需要遮阳伞";
                    }else{
                        res += "你不需要遮阳伞";
                    }
                    massage.setText(res);
                    break;
                case R.id.cleartext:
                    massage.setText("信息显示区...");
                    for(int i = 0; i < checkBoxNum; i += 1){
                        checkBoxGroup[i].setChecked(false);
                    }
                    if(radioGroup.getCheckedRadioButtonId() >= 0){
                        ((RadioButton) findViewById(radioGroup.getCheckedRadioButtonId())).setChecked(false);
                    }
                    switch01.setChecked(false);
                    switch02.setChecked(false);
                    break;
            }
        }
    };

    private CompoundButton.OnCheckedChangeListener QswitchChangeListener
            = new CompoundButton.OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton cb, boolean bool){
            int id = cb.getId();
            switch (id){
                case R.id.switch01:
                    if(bool){
                        switchT01.setText("需要排队板凳:是");
                    }else{
                        switchT01.setText("需要排队板凳:否");
                    }
                    break;
                case R.id.switch02:
                    if(bool) {
                        switchT02.setText("需要遮阳伞：是");
                    }else{
                        switchT02.setText("需要遮阳伞：否");
                    }
                    break;
                case R.id.toggleButton:
                    if(bool){
                        togT.setText("窄屏模式：开启");
                        linear01.setOrientation(LinearLayout.VERTICAL);
                        //linear01.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
                    }else{
                        togT.setText("窄屏模式：关闭");
                        linear01.setOrientation(LinearLayout.HORIZONTAL);
                    }
                    break;
            }
        }
    };

}
